﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SD.DataAccess
{
    partial class Program
    {
        private readonly ServiceDBDataContext dataContext = new ServiceDBDataContext();

        /// <summary>
        /// add new program to database
        /// </summary>
        /// <param name="newProgram"> an instance of program class </param>
        /// <returns> true or false </returns>
        public bool AddProgram(Program newProgram)
        {
            try
            {
                dataContext.Programs.InsertOnSubmit(newProgram);
                dataContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// edit new program to database
        /// </summary>
        /// <param name="newProgram"> an instance of program class </param>
        /// <returns> true or  false </returns>
        public bool AmendProgram(Program newProgram)
        {
            Program program = dataContext.Programs.FirstOrDefault(item => item.ProgramId == newProgram.ProgramId);
            if (program != null)
            {
                program.ProgramName = newProgram.ProgramName;
                program.Description = newProgram.Description;
                program.ContactId = newProgram.ContactId;
                program.IsActive = newProgram.IsActive;
                try
                {
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit status program to database
        /// </summary>
        /// <param name="programId"> programId want to edit status </param>
        /// <param name="status"> status program want to change </param>
        /// <returns> true or false </returns>
        public bool ChangeProgramStatus(int programId, bool status)
        {
            Program newProgram = dataContext.Programs.FirstOrDefault(item => item.ProgramId == programId);
            if (newProgram != null)
            {
                try
                {
                    newProgram.IsActive = status;
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// get list program 
        /// </summary>
        /// <param name="status"> status want to select </param>
        /// <param name="filter"> type of filter </param>
        /// <returns> list program </returns>
        public IQueryable<ProgramView> ListProgram(bool status, Collection<string> filter)
        {
            IQueryable<ProgramView> newProgram;
            if (filter == null)
            {
                //show program have IsActive = true
                if (status == true)
                {
                    newProgram = dataContext.ProgramViews;
                }
                // show all program
                else
                {
                    newProgram = dataContext.ProgramViews.Where(item => item.IsActive == true).OrderBy(item => item.ProgramId);
                }
            }
            else
            {
                //show program have IsActive = true
                if (status == true)
                {
                    newProgram = dataContext.ProgramViews.Where(item => filter.Contains(item.ProgramName[0].ToString())).OrderBy(item => item.ProgramId);
                }
                // show all program
                else
                {
                    newProgram = dataContext.ProgramViews.Where(item => item.IsActive == true && filter.Contains(item.ProgramName[0].ToString())).OrderBy(item => item.ProgramId);
                }
            }
            return newProgram;
        }

        /// <summary>
        /// get program selected
        /// </summary>
        /// <param name="programId"> programId selected </param>
        /// <returns> program </returns>
        public ProgramView GetProgram(int programId)
        {
            ProgramView newProgram = dataContext.ProgramViews.FirstOrDefault(item => item.ProgramId == programId);
            return newProgram;
        }

        /// <summary>
        /// check name has already existed ?
        /// </summary>
        /// <param name="name"> name want to check </param>
        /// <returns> true or false </returns>
        public bool CheckNameProgram(string name)
        {
            Program newProgram = dataContext.Programs.FirstOrDefault(item => item.ProgramName == name);
            if (newProgram != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
